<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="rolePage">
        <el-row>
          <el-col :sm="4">
            <div class="g-title">排产计划</div>
          </el-col>
          <el-col :sm="20" style="text-align: right">
            <span class="role-add" @click="handleCreate()"><i class="el-icon-plus" />添加</span>
          </el-col>
        </el-row>

        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          @change="fetchData()"
        />
      </div>
    </div>
    <el-row class="table">
      <el-table
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >

        <el-table-column prop="scheduleDate" label="日期" show-overflow-tooltip align="center" />
        <el-table-column prop="workLines" label="线别" show-overflow-tooltip align="center" />
        <el-table-column prop="schedulePlan" label="计划投入" show-overflow-tooltip align="center" />
        <el-table-column prop="scheduleActual" label="实际投入" show-overflow-tooltip align="center" />
        <el-table-column prop="scheduleAchiRates" label="计划达成率" show-overflow-tooltip align="center" />
        <el-table-column prop="actualInStorage" label="实际入库" show-overflow-tooltip align="center" />
        <el-table-column prop="wipProducts" label="WIP" show-overflow-tooltip align="center" />
        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button
              v-if="row.status != 'deleted'"
              size="mini"
              type="danger"
              @click="handleDelete(row, $index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          />
        </el-col>
      </el-row>
    </el-row>

    <!-- 添加修改弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="temp.date"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="线别" prop="workLines">
          <el-input v-model="temp.workLines" placeholder="请输入内容" clearable />
        </el-form-item>
        <el-form-item label="计划投入" prop="schedulePlan">
          <el-input v-model="temp.schedulePlan" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="实际投入" prop="scheduleActual">
          <el-input v-model="temp.scheduleActual" placeholder="请输入内容" clearable @change="handleInputChange" />
        </el-form-item>
        <el-form-item label="计划达成率" prop="scheduleAchiRates">
          <el-input v-model="temp.scheduleAchiRates" placeholder="请输入计划投入和实际投入" clearable :disabled="true" />
        </el-form-item>
        <el-form-item label="实际入库" prop="actualInStorage">
          <el-input
            v-model="temp.actualInStorage"
            placeholder="请输入内容"
            clearable
            @change="handleInputChange"
          />
        </el-form-item>
        <el-form-item label="WIP" prop="wipProducts">
          <el-input v-model="temp.wipProducts" placeholder="请输入内容" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          {{ textMap[dialogStatus] }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import './css/index.scss'
import { scheduleAllItems, scheduleAdd, deleteSchedule, updateSchedule } from '@/api/report'
export default {
  data: function() {
    return {
      activeCardName: 'all',
      dialogFormVisible: false,
      dialogSee: false,
      dialogStatus: '',
      dateRange: null,
      singleTime: '',
      langtopicTypeData: [],
      textMap: {
        update: '修改',
        create: '添加'
      },
      SeeData: {
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      },
      temp: {
        meetingId: '',
        workLines: '',
        plannedInputs: '',
        actualInputs: '',
        actualInStorage: '',
        wipProducts: '',
        goodRates: '',
        workLossRates: '',
        originalLossRates: '',
        chuanReturnRates: '',
        dieReturnRates: '',
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      },
      rules: {
        date: [{ required: true, message: '日期没有选择', trigger: 'change' }],
        workLines: [{ required: true, message: '线别没有填写', trigger: 'blur' }],
        schedulePlan: [{ required: true, message: '计划投入没有填写', trigger: 'blur' }],
        scheduleActual: [{ required: true, message: '实际投入没有填写', trigger: 'blur' }],
        actualInStorage: [{ required: true, message: '实际入库没有填写', trigger: 'blur' }],
        wipProducts: [{ required: true, message: 'WIP没有填写', trigger: 'blur' }]

      },
      total: 0,
      pageNo: 1,
      pageSize: 10,
      list: [], // 全部数据
      listDataSelected: [] // 选中数据
    }
  },
  created() {
    // 获取数据
    this.fetchData()
  },
  methods: {
    // 查询

    // 重置数据
    resetTemp() {
      this.temp = {
        meetingId: '',
        workLines: '',
        plannedInputs: '',
        actualInputs: '',
        actualInStorage: '',
        wipProducts: '',
        goodRates: '',
        workLossRates: '',
        originalLossRates: '',
        chuanReturnRates: '',
        dieReturnRates: '',
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      }
    },
    // 计算计划达成率
    handleInputChange() {
      console.log('temp', this.temp)
      if (this.temp.schedulePlan && this.temp.scheduleActual) {
        this.temp.scheduleAchiRates = ((this.temp.scheduleActual / this.temp.schedulePlan) * 100).toFixed(1) + '%'
        this.$forceUpdate()
      }
    },
    // 打开添加早会弹窗
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    // 添加早会提交数据
    createData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const date = this.getTime(this.temp.date)
          const data = {
            scheduleDate: date,

            schedulePlan: this.temp.schedulePlan,
            scheduleActual: this.temp.scheduleActual,
            scheduleAchiRates: this.temp.scheduleAchiRates,
            wipProducts: this.temp.wipProducts,
            actualInStorage: this.temp.actualInStorage,
            workLines: this.temp.workLines
          }
          await scheduleAdd(data).then((r) => {
            console.log('temp', this.temp)
            this.dialogFormVisible = false
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message + '需要修改' + r.data
              })
            }
          })
        }
      })
    },
    //  修改早会
    handleUpdate(row) {
      const data = Object.assign({}, row)
      this.temp = {
        scheduleId: data.scheduleId,
        workLines: data.workLines,
        date: data.scheduleDate,
        schedulePlan: data.schedulePlan,
        scheduleActual: data.scheduleActual,
        scheduleAchiRates: data.scheduleAchiRates,
        wipProducts: data.wipProducts,
        actualInStorage: data.actualInStorage
      }
      console.log('temp', this.temp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改早会提交数据
    updateData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const date = this.getTime(this.temp.date)

          const data = {
            scheduleDate: date,
            scheduleId: this.temp.scheduleId,
            schedulePlan: this.temp.schedulePlan,
            scheduleActual: this.temp.scheduleActual,
            scheduleAchiRates: this.temp.scheduleAchiRates,
            wipProducts: this.temp.wipProducts,
            actualInStorage: this.temp.actualInStorage,
            workLines: this.temp.workLines
          }
          console.log('data', data)
          await updateSchedule(data).then((r) => {
            this.dialogFormVisible = false
            this.singleTime = this.temp.singleTime
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 删除早会
    async handleDelete(row) {
      await deleteSchedule({ id: row.scheduleId }).then((r) => {
        if (r && r.code == 200) {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: r.message
          })
        }
      })
    },
    // 分页组件
    pageSizeChange(newSize) {
      this.pageSize = newSize
      this.pageNo = 1
      this.fetchData()
    },
    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo
      }
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,

        starTime: '',
        endTime: ''
      }
      if (this.dateRange) {
        const start = this.getTime(this.dateRange[0])
        const end = this.getTime(this.dateRange[1])
        params.starTime = start
        params.endTime = end
      }
      const data = await scheduleAllItems(params)
      this.list = data.data.content
      this.total = data.data.totalSize
    },
    // 表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0)
      val.forEach(e => {
        this.listDataSelected.push(e)
      })
    },
    getTime(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      return y + '-' + m + '-' + d
    }
  }
}
</script>
<style lang="scss" scoped>
.userlist {
    margin-right: 10px;
}

.title {
    height: 20px;
    padding-left: 5px;
    line-height: 20px;
    font-size: 20px;
    font-weight: bold;
    border-left: 7px solid #1587F9;
    margin-bottom: 23px;
    display: inline-block;
}
</style>
